<template>
  <div>
    <el-form
      ref="ruleForm"
      :model="subject"
      status-icon
      :rules="rules"
      label-width="80px"
    >
      <el-form-item label="科目ID" prop="subjectNo">
        <el-input
          readonly
          v-model="subject.subjectNo"
          placeholder="请输入编号"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="科目名称" prop="subjectName">
        <el-input
          v-model="subject.subjectName"
          placeholder="请输入名称"
        ></el-input>
      </el-form-item>
      <!-- <el-form-item label="科目班级" prop="subjectClass">
        <el-select v-model="sizeForm.subjectClass" placeholder="请选择班级">
          <el-option label="九年一班" value="0"></el-option>
          <el-option label="九年二班" value="1"></el-option>
           <el-option label="九年三班" value="2"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { nanoid } from "nanoid";
import rules from "../commons/validate"

import {
  useGetSubject,
  useUpdateSubject
} from "@/hooks/subject";

export default {
  name: "SubjectEdit",
  props:["id"],
  data() {
    return {
      rules,
      subject: {
        subjectNo:"",
        subjectName:""
      },
    };
  },

  mounted() {
    (async () => {
      if (this.$route.params.id) {
        const resInfo = await useGetSubject(this.$route.params.id);
        this.subject = resInfo.data;
        if (resInfo.code !== 201 || resInfo.message != "success") {
          this.$message.error("未找到科目信息，ID不正确");
        }
      } else {
        this.$message.error("未传递学院ID，未找到学院信息");
      }
    })();
  },

  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
              
              const resInfo = await useUpdateSubject(this.subject);
          if (resInfo.code === 201 && resInfo.message === "success") {
            this.$message.success("修改成功");
          } else {
            this.$message.error(resInfo.message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="scss" scoped></style>
